package com.yixuan.mt.console.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yixuan.mt.common.entity.Policy;
import com.yixuan.mt.common.exception.HttpBadRequestException;
import com.yixuan.mt.common.response.Result;
import com.yixuan.mt.common.request.PostPoliciesRequest;
import com.yixuan.mt.common.service.PoliciesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/op/policies")
public class PoliciesController {

    @Autowired
    private PoliciesService policiesService;

    /**
     * 新增策略
     * @throws HttpBadRequestException 策略名重复
     */
    @PostMapping
    public Result<?> postPolicies(@RequestBody PostPoliciesRequest postPoliciesRequest) {
        policiesService.postPolicies(postPoliciesRequest);
        return Result.success();
    }

    /**
     * 删除策略
     */
    @DeleteMapping("/{id}")
    public Result<?> deletePolicies(@PathVariable Integer id) throws JsonProcessingException {
        policiesService.deletePolicies(id);
        return Result.success();
    }

    /**
     * 修改策略内容
     */
    @PutMapping
    public Result<?> putPolicies(@RequestBody Policy policy) throws JsonProcessingException {
        policiesService.putPolicies(policy);
        return Result.success();
    }

    /**
     * 获取策略列表
     */
    @GetMapping
    public Result<List<Policy>> getPoliciesList() {
        return Result.success(policiesService.getPoliciesList());
    }

    /**
     * 根据id获取策略详情（内容）
     */
    @GetMapping("/detail/{id}")
    public Result<String> getPolicyDetail(@PathVariable Integer id) {
        return Result.success(policiesService.getPolicyDetail(id));
    }

}
